package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired//es高级客户端
    private RestHighLevelClient restHighLevelClient;

    @Autowired//spring 官方客户端//封装es
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ListService listService;

    @ApiOperation("创建主页索引")
    @GetMapping("/createIndex")
    public Result createIndex(){
        //创建索引
        //创建索引 2.type  3.Mapping
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);

        return Result.ok();
    }

    //导入索引
    @ApiOperation("导入索引")
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }

    //删除索引
    @ApiOperation("删除索引")
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }

    @ApiOperation("热度评分")
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }

    @ApiOperation("搜索ES索引库")
    @PostMapping("/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        SearchResponseVo responseVo=listService.search(searchParam);
        return responseVo;
    }
}
